package com.chinaoct.siku.gzw.services;

import com.chinaoct.siku.LocalStorage.dao.StorageDTO;
import com.chinaoct.siku.gzw.dao.ShangbaoDTO;
import com.chinaoct.siku.gzw.entity.Shangbao;
import com.chinaoct.siku.gzw.report.GzwDbReport;
import com.chinaoct.siku.gzw.repository.ShangbaoRep;
import com.chinaoct.siku.util.UnzipUtility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ShangbaoServiceImpl implements ShangbaoService {

    @Autowired
    private ShangbaoRep shangbaoRep;
    @Value("${gzw.file_path}")
    private String filePath;
    @Autowired
    private GzwDbReport gzwDbReport;

    @Override
    public void save(ShangbaoDTO shangbaoDTO) {
        Shangbao shangbao = new Shangbao();
        BeanUtils.copyProperties(shangbaoDTO,shangbao);
        if(null == shangbao.getCreateTIme()){
            shangbao.setCreateTIme(new Date());
        }
        shangbaoRep.save(shangbao);
        shangbaoDTO.setId(shangbao.getId());
    }

    @Override
    public void delete(Long id) {
        shangbaoRep.deleteById(id);
    }

    @Override
    public Page<ShangbaoDTO> pageShangbao(Pageable pageable) {
        Specification<Shangbao> specification = new Specification<Shangbao>() {
            @Override
            public Predicate toPredicate(Root<Shangbao> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                //                if(!StringUtils.isEmpty(param.getUnitId())){
                //                    list.add(cb.equal(root.get("unitId"),param.getUnitId()));
                //                }
                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
        Page<Shangbao> pages=shangbaoRep.findAll(specification,pageable);
        List<Shangbao> shangbaoList=pages.getContent();
        List<ShangbaoDTO> shangbaoDTOS=new ArrayList<>();
        for(Shangbao shangbao:shangbaoList){
            ShangbaoDTO dto=new ShangbaoDTO();
            BeanUtils.copyProperties(shangbao,dto);
            shangbaoDTOS.add(dto);
        }
        PageImpl<ShangbaoDTO> results= new PageImpl(shangbaoDTOS,pages.getPageable(),pages.getTotalElements());
        return results;
    }

    @Override
    public ShangbaoDTO findByID(Long id) {
        Shangbao shangbao = shangbaoRep.getOne(id);
        if(null!=shangbao){
            ShangbaoDTO shangbaoDTO =new ShangbaoDTO();
            BeanUtils.copyProperties(shangbao,shangbaoDTO);
            return shangbaoDTO;
        }
        return null;
    }

    @Override
    public boolean uploadFile(StorageDTO storageDTO, String dataDate,Integer enableFile) throws Exception {
        if (null == storageDTO || StringUtils.isEmpty(storageDTO.getPath())){
            return false;
        }
        Date startTime = new Date();
        String ptah=filePath+System.currentTimeMillis();
        File file = new File(ptah);
        file.mkdir();
        List<String> list = UnzipUtility.unZip(new File(storageDTO.getPath()), ptah);
        try {
            Map<String, String> map = gzwDbReport.report(ptah, list,dataDate,enableFile);
            ShangbaoDTO shangbaoDTO = new ShangbaoDTO();
            shangbaoDTO.setPackage1162(map.get("1162"));
            shangbaoDTO.setMsg1162("success");
            shangbaoDTO.setPackage1168(map.get("1168-1"));
            shangbaoDTO.setMsg1168("success");
            shangbaoDTO.setPackageFile1168(map.get("1168-2"));
            shangbaoDTO.setStatus(1);
            shangbaoDTO.setDataDate(dataDate);
            shangbaoDTO.setStartTime(startTime);
            this.save(shangbaoDTO);
            return true;
        }finally {
            if(null!=file){
                File[] files = file.listFiles();
                for(File file1:files){
                    if(file1.isDirectory()){
                        File[] listFiles = file1.listFiles();
                        for(File f:listFiles){
                            f.delete();
                        }
                    }
                    file1.delete();
                }
                if(file.exists()){
                    file.delete();
                }
            }
        }
    }
}
